//****************针对第二种方式的具体js实现部分******************//
//****************所使用的数据是city.js******************//
/*根据id获取对象*/
var serUrl =window.location.origin
/*var serUrl ='http://192.0.11.154'*/
function ById(str) {
    return document.getElementById(str);
}
var serverAddr =''
var appToken = ''
ById('randomSize').onblur = function(e){
    var e = e || window.event;
    var target = e.target || e.srcElement;
    var newVal = '';
    newVal = target.value.replace (/[^/0-9]/ig, '')
    newVal = newVal.replace(/\s+/ig, '');
    if (newVal.length > 5) {
        newVal = newVal.substring(0, 5)
    }
    if(newVal < target.min){
        newVal = target.min
    }
    if(newVal > target.max){
        newVal = target.max
    }
    document.getElementById('randomSize').value = newVal
}
ById('idSize').onblur = function(e){
    var e = e || window.event;
    var target = e.target || e.srcElement;
    var newVal = '';
    newVal = target.value.replace (/[^/0-9]/ig, '')
    newVal = newVal.replace(/\s+/ig, '');
    if (newVal.length > 5) {
        newVal = newVal.substring(0, 5)
    }
    if(newVal < target.min){
        newVal = target.min
    }
    if(newVal > target.max){
        newVal = target.max
    }
    document.getElementById('idSize').value = newVal
}
assignServerNode()
function assignServerNode(){
    var prams={"appId":"84ef735cf952406288354c6da42185ea","appSecret":"1bf04a62cc4344cda0261efb581045ac",serverType:1}
    $.ajax({
        url:serUrl+'/qrmc/nodeexternal/assignServerNodeExternal',    //此处连接地址可变更
        type: 'post',
        data: JSON.stringify(prams),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        async: true,
        crossDomain: true === !(document.all),
        success: function (response) {
            if(response.code === 0 ){
                serverAddr =window.location.protocol +'//'+ response.data.serverAddr
                appToken = response.data.appToken
            }else{
                serverAddr=''
                appToken=''
                ngTip.tip(response.message, 'warning')
            }
        },
        error:function ( response ) {
            ngTip.tip(response.message, 'warning')
        }
    })
}

ById('createRandom').onclick = function (e) {
    let randomSize = ById('randomSize').value
    if(randomSize === ''){
        ngTip.tip ('请输入AASTU随机数生成长度！', 'warning')
        return
    }
    var pra = {
        reqLen:Number(ById('randomSize').value),
        appToken:appToken,
    }
    ById('spinBox').style.display = 'inline-block'
    ById('showRandom').innerHTML = ''
    $.ajax({
        url:serverAddr+'/qrngser/genQRandom',    //此处连接地址可变更
        type: 'post',
        data: JSON.stringify(pra),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        crossDomain: true,
        success: function (res) {
            if(res.code === 0 ){
                ById('showRandom').innerHTML = res.data
            }else{
                //ngTip.tip(res.message, 'warning')
            }
            ById('spinBox').style.display = 'none'
        },
        error:function ( res ) {
            ngTip.tip('请求错误', 'warning')
            ById('spinBox').style.display = 'none'
        }
    })
}

ById('createRandomID').onclick = function (e) {
    let randomSize = ById('idSize').value
    if(randomSize === ''){
        ngTip.tip ('请输入AASTU随机ID生成长度！', 'warning')
        return
    }
    var pra = {
        reqIDSize:Number(ById('idSize').value),
        appToken:appToken,
    }
    ById('showRandomT').innerHTML = ''
    ById('idWidth').value = ''
    ById('showIdWidth').style.display = 'none'
    ById('spinBox').style.display = 'inline-block'
    $.ajax({
        url:serverAddr+'/qrngser/getQRandomId',    //此处连接地址可变更
        type: 'post',
        data: JSON.stringify(pra),
        contentType:"application/json;charset=UTF-8",
        dataType:"json",
        crossDomain: true,
        success: function (res) {
            if(res.code === 0 ){
                ById('showRandomT').innerHTML = res.data.data
                ById('showIdWidth').style.display = 'inline-block'
                ById('idWidth').value = res.data.idWidth
            }else{
                ngTip.tip(res.message, 'warning')
            }
            ById('spinBox').style.display = 'none'
        },
        error:function ( res ) {
            ngTip.tip('请求错误', 'warning')
            ById('spinBox').style.display = 'none'
        }
    })
}

function downLoadDAT(){
    let ss = ById('showRandom').innerHTML
    if(ss.length > 0){
        const blob = new Blob([ss])
        const url =URL.createObjectURL(blob)
        const element = document.createElement('a')
        element.style.display = 'none'
        element.href= url
        element.download =ById('randomSize').value+'random.DAT'
        element.click()
    }else{
        ngTip.tip('请生成AASTU随机数内容！', 'warning')
    }
}


function downLoadDATT(){
    let ss = ById('showRandomT').innerHTML
    if(ss.length > 0){
        const blob = new Blob([ss])
        const url =URL.createObjectURL(blob)
        const element = document.createElement('a')
        element.style.display = 'none'
        element.href= url
        element.download =ById('idSize').value+'randomID.DAT'
        element.click()
    }else {
        ngTip.tip('请生成AASTU随机ID内容！', 'warning')
    }
}
